﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZHDJ.Data
{
    public class Utility
    {
        /// <summary>
        /// 获取指定级数的父节点
        /// </summary>
        /// <param name="branchID"></param>
        /// <returns></returns>
        public static string GetParentBranch(string branchID)
        {
            string sql = "SELECT ParentBranchID FROM tblpartyorgbranch WHERE BranchID ='" + branchID + "'";
            object obj =MySqlHelper.ExecuteScalar(sql);
            if (obj != null) return obj.ToString();
            else return "";
        }

        /// <summary>
        /// 返回orderno+1
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="orderNoField"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static int GetSortOrderNo(string tableName, string orderNoField, string filter)
        {
            string text = string.Format("select MAX({1}) from {0}", tableName, orderNoField);
            if (!string.IsNullOrEmpty(filter))
            {
                text = text + " where " + filter;
            }
            object obj = MySqlHelper.ExecuteScalar(text);
            if (obj != null && obj.ToString() != "")
            {
                return Convert.ToInt32(obj.ToString()) + 1;
            }
            return 0;
        }

        /// <summary>
        /// 检查是否存在唯一 false 存在，true 不存在
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fieldValue"></param>
        /// <param name="otherFilter"></param>
        /// <returns></returns>
        public static bool CheckFieldUnique(string tableName, string fieldName, string fieldValue, string otherFilter)
        {
            bool result;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendFormat("SELECT COUNT(*) FROM {0} WHERE {1} ='{2}' ", tableName, fieldName, fieldValue);
                if (!string.IsNullOrEmpty(otherFilter))
                {
                    stringBuilder.Append(" AND " + otherFilter);
                }
                object obj = MySqlHelper.ExecuteScalar(stringBuilder.ToString());
                if (obj != null && obj.ToString() != "" && Convert.ToInt32(obj) > 0)
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
            
            return result;
        }

        public static bool CheckFieldUnique(string tableName, string fieldName, string fieldValue, string keyName, string keyValue, string otherFilter)
        {
            string text = string.Format("{0}<>'{1}'", keyName, keyValue);
            if (!string.IsNullOrEmpty(otherFilter))
            {
                text = text + " and " + otherFilter;
            }
            return CheckFieldUnique(tableName, fieldName, fieldValue, text);
        }



    }
}
